package com.hdy.orderforthree.widget;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hdy.net.order.NetCallback;
import com.hdy.net.order.bean.LoginBean;
import com.hdy.net.order.bean.TablesInfoResponse;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;
import com.hdy.orderforthree.pos.ConcretePosFactory;

import butterknife.BindView;

public class LoginBasePop extends FullContentBasePop {

    @BindView(R2.id.login_start)
    TextView mStartLogin;
    @BindView(R2.id.login_name_et)
    EditText mLoginName;
    @BindView(R2.id.login_pwd_et)
    EditText mLoginPassword;
    @BindView(R2.id.login_ip_et)
    EditText mLoginIp;
    public LoginBasePop(int layoutId, Context context) {
        super(layoutId, context);

        mLoginPassword.setText("");
        mStartLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mLoginName.getText().toString();
                String pwd = mLoginPassword.getText().toString();
                String ip = mLoginIp.getText().toString();

                if (name == null || pwd == null || ip == null
                        || name.length() == 0 || pwd.length() == 0 || ip.length() == 0) {
                    Toast.makeText(context, context.getString(R.string.login_toast), Toast.LENGTH_SHORT).show();
                    return;
                }

                LoginBean loginBean = new LoginBean();
                loginBean.setIp(ip);
                loginBean.setPassword(pwd);
                loginBean.setName(name);
                ConcretePosFactory.getInstance().getPos().login(loginBean, new NetCallback<TablesInfoResponse>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(context, context.getString(R.string.login_shop_error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(TablesInfoResponse o) {
                        if (o.isSuccess()) {
                            ConcretePosFactory.getInstance().getPos().saveLoginBean(loginBean);
                            if (netCallback != null) {
                                netCallback.onNext(o);
                            }
                        } else {
                            Toast.makeText(context, o.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
            }
        });
    }
}
